package com.zzyl.serve.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.*;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingProjectVo;
import org.apache.xmlbeans.impl.xb.xsdschema.WhiteSpaceDocument;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //获取计划表
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
          BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        //获取计划的所有的中间表


        // 获取所有的中间表
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.selectId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);


        return   nursingPlanVo;

    }

    /**
     *
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param//nursingPlann 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //新增 护理计划
        NursingPlan nursingPlan = new NursingPlan();
        //将数据nursingPlan复制到dto中
        BeanUtils.copyProperties(dto, nursingPlan);
        // 批量新增 护理项目
        save(nursingPlan);
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();

        Integer integer = nursingProjectPlanMapper.addBatch( projectPlans,nursingPlan.getId() );
        return  integer;
    }

    /**
     * 修改护理计划
     * 
     * @param 、、nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {

        //拿到中间表
        NursingPlan nursingPlan = new NursingPlan();
        //将数据nursingPlan复制到dto中
        BeanUtils.copyProperties(dto, nursingPlan);
        //判断中间表是否有数据
        nursingPlan.setId(dto.getId());
        if ( dto.getProjectPlans() != null&& dto.getProjectPlans().size() > 0) {
            //修改计划表
            nursingProjectPlanMapper.deleteByIdAll(nursingPlan.getId());
            nursingProjectPlanMapper.addBatch( dto.getProjectPlans() , nursingPlan.getId());
        }
        //修改中间表先删除在增加
        return nursingPlanMapper.updateNursingPlan(nursingPlan);

    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {

        NursingPlan nursingPlan = new NursingPlan();
        //删除中间表
        nursingProjectPlanMapper.deleteByIdAll(nursingPlan.getId());
        //删除计划表
        return removeById(id) ? 1 : 0;
    }

}
